package com.cmw.thread.concurrent.l1;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * //调用await()方法的线程会被挂起，它会等待直到count值为0才继续执行
 * public void await() throws InterruptedException { };
 * //和await()类似，只不过等待一定的时间后count值还没变为0的话就会继续执行
 * public boolean await(long timeout, TimeUnit unit) throws InterruptedException { };
 * //将count值减1
 * 参考：https://www.jianshu.com/p/e233bb37d2e6
 * public void countDown() { };
 * @ClassName CountDownLatchTest
 * @Description: TODO
 * @Author cheng
 * @Date 2020/6/29
 * @Version V1.0
 **/
public class CountDownLatchTest {

    public static void main(String[] args) {
        final CountDownLatch latch = new CountDownLatch(2);
        System.out.println("主线程开始执行…… ……");
        ExecutorService es1 = Executors.newSingleThreadExecutor();
        es1.execute(()->{
            try {
                Thread.sleep(3000);
                System.out.println("子线程："+Thread.currentThread().getName()+"执行");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            latch.countDown();
        });
        es1.shutdown();

        ExecutorService es2 = Executors.newSingleThreadExecutor();
        es2.execute(()->{
            try {
                Thread.sleep(3000);
                System.out.println("子线程："+Thread.currentThread().getName()+"执行");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            latch.countDown();
        });
        es2.shutdown();
        System.out.println("等待两个线程执行完毕... ... ");
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("两个子线程都执行完毕，继续执行主线程");
    }
}
